package dialer.ws.business.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

import dialer.ws.base.entity.Form;

public class FormConverter implements Converter {

	@SuppressWarnings("unchecked")
	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
		if (value == null || value.length() == 0) {
			return null;
		}

		PickList pl = (PickList) component;
		DualListModel<Form> dl = (DualListModel<Form>) pl.getValue();
		for (Form f : dl.getSource()) {
			if (f.getId().equals(Integer.valueOf(value))) {
				return f;
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
		if (object == null) {
			return null;
		}
		if (object instanceof Form) {
			Form f = ((Form) object);
			return String.valueOf(f.getId());
		}
		return null;
	}

}
